package cn.kone.blog.service.impl;


import cn.kone.blog.domain.Param;
import cn.kone.blog.domain.query.ParamQuery;
import cn.kone.blog.mapper.ParamMapper;
import cn.kone.blog.service.IParamService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ParamServiceImpl implements IParamService {
    @Autowired
    private ParamMapper paramMapper;


    //查询单个
    @Override
    public Param findOne(Long id) {
        return paramMapper.findOne(id);
    }

    //查询全部
    @Override
    public List<Param> findAll() {

        return paramMapper.findAll();

    }

    //新增
    @Override
    @Transactional
    public void addParam(Param param) {

        paramMapper.addParam(param);

    }

    //修改
    @Override
    @Transactional
    public void updateParam(Param param) {
        param.setUpdateTime(new Date());
        paramMapper.updateParam(param);
    }


    //删除
    @Override
    @Transactional
    public void deleteParam(Long id) {
        paramMapper.deleteParam(id);
    }

    //分页+高级查询
    @Override
    public List<Param> pageQuery(ParamQuery paramQuery) {
        PageHelper.startPage(paramQuery.getCurrentPage(), paramQuery.getPageSize());
        return paramMapper.pageQuery(paramQuery);
    }

    //批量删除
    @Override
    @Transactional
    public void batchDeleteParam(Long[] ids) {
        paramMapper.batchDeleteParam(ids);
    }

    @Override
    public Map<String, String> paramAll() {
        List<Param> list = paramMapper.findAll();
        Map<String, String> map = list.stream().collect(Collectors.toMap(
                param -> param.getParamKey(),
                param -> param.getParamValue()
        ));

        return map;
    }


}
